import geoip2.database


def geo(allDate):
    l = []
    list = []
    reader = geoip2.database.Reader("scan/component/GeoLite2-City.mmdb")
    # 将所查询的IP地址读取出来赋值给data
    for i in range(len(allDate)):
        ip = allDate[i]['host']
        data = reader.city(ip)
        list.append(data.city.names.get('zh-CN'))

    map = {}
    for i in list:
        # 通过key来计算元素个数
        map[i] = map.get(i, 0) + 1
    l.append(map)
    return l


def getCoordinate(ip):
    reader = geoip2.database.Reader("scan/component/GeoLite2-City.mmdb")
    try:
        data = reader.city(ip)
        return [data.location.latitude, data.location.longitude]
    except:
        return [None, None]
